package com.keguoyu.easymvp.ioc;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import com.keguoyu.easymvp.annotation.ForInvoker;
import com.keguoyu.easymvp.commons.PluginFactory;

public final class PluginConfig {
  public static final String INVOKER_ID = "PLUGIN_REG";
  private static final Map<Class<? extends PluginInterface>, Collection<ImplMapper>> mappers = new HashMap<>();

  public static Map<Class<? extends PluginInterface>, Collection<ImplMapper>> getMappingMap() {
    doRegister();
    return mappers;
  }

  @ForInvoker(methodId = INVOKER_ID)
  private static void doRegister(){
  }

  public static <T extends PluginInterface> void register(Class<T> cls, PluginFactory<? extends T> factory) {
      register(cls, factory, 1);
  }

  public static <T extends PluginInterface> void register(Class<T> cls, PluginFactory<? extends T> factory, int priority) {
      Collection<ImplMapper> implMappers = mappers.get(cls);
      if (implMappers == null) {
        implMappers = new HashSet<>();
        mappers.put(cls, implMappers);
      }
      implMappers.add(new ImplMapper(cls, factory, priority));
    }

}
